﻿using System.Globalization;
using UnityEditor;
using UnityEngine;

namespace Yoozoo.Modules.Furnitures.Editor
{
    [CustomEditor(typeof(Furniture))]
    public class FurnitureEditor : UnityEditor.Editor
    {
        private Furniture furniture;

        private int heightIndex = 2;

        void OnEnable()
        {
            furniture = (Furniture)this.target;
        }

        void OnSceneGUI()
        {
            HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
            int id = GUIUtility.GetControlID(FocusType.Passive);
            EventType type = Event.current.GetTypeForControl(id);


            if (type == EventType.Repaint)
            {
                //Handles.SphereHandleCap(0, _pointInGrid, Quaternion.identity, 0.2f, EventType.Repaint);
                //Handles.Label(_pointInGrid + Vector3.right, "点击右键删除，左键确认新位置");
            }

            HandleUtility.Repaint();
        }

        public void DrawLocationButton(Vector2 location, string buttonName)
        {
            if (furniture.Contains(location))
            {
                if (GUILayout.Button("*" + buttonName))
                {
                    furniture.RemoveLocation(location);
                    EditorUtility.SetDirty(furniture);
                }
            }
            else
            {
                if (GUILayout.Button(buttonName))
                {
                    furniture.AddLocation(location);
                    EditorUtility.SetDirty(furniture);
                }
            }
        }

        public override void OnInspectorGUI()
        {
            GUILayout.Label("家具编辑器");
            var boxCollider = furniture.GetComponent<BoxCollider>();
            if (boxCollider == null)
            {
                if (GUILayout.Button("创建碰撞"))
                {
                    boxCollider = furniture.gameObject.AddComponent<BoxCollider>();
                    var size = new Vector3(furniture.X * 0.5f, 1, furniture.Y * 0.5f);
                    boxCollider.size = size;
                    boxCollider.center = new Vector3(
                        size.x * (0.5f - furniture.Pivot.x),
                        size.y * 0.5f,
                        size.z * (0.5f - furniture.Pivot.y));
                    EditorUtility.SetDirty(furniture);
                }
            }
            else
            {
                GUILayout.Label("设置碰撞盒高度(单位m)");
                float[] heights = { 0.25f, 0.5f, 1f, 2f, 3f };
                string[] toolbar = new string[heights.Length];
                for (int i = 0; i < heights.Length; i++)
                {
                    toolbar[i] = heights[i].ToString(CultureInfo.InvariantCulture);
                }
                int index = GUILayout.Toolbar(heightIndex, toolbar);
                if (index != heightIndex)
                {
                    heightIndex = index;
                    float height = heights[heightIndex];
                    var size = boxCollider.size;
                    size.y = height;
                    boxCollider.size = size;
                    var center = boxCollider.center;
                    center.y = height * 0.5f;
                    boxCollider.center = center;
                    EditorUtility.SetDirty(furniture);
                }
            }

            if (GUILayout.Button("设置层级"))
            {
                furniture.SetLayer(LayerMask.NameToLayer("commander1"));
                EditorUtility.SetDirty(furniture);
            }

            GUILayout.Label("交互点");
            if (GUILayout.Button("全选"))
            {
                SelectAll();
            }
            if (GUILayout.Button("全不选"))
            {
                DeselectAll();
            }

            //         (0, Y)     (X-1, Y)
            // (-1, Y-1) p3 --------- p2 (X, Y-1)
            //           |            |
            // (-1, 0)   p0 --------- p1 (X, 0)
            //         (0, -1)       (X-1, -1)

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            var location = new Vector2(0, 0);
            for (int x = 0; x < furniture.X; x++)
            {
                location.x = x;
                location.y = furniture.Y;
                DrawLocationButton(location, "T" + (x + 1));
            }
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            for (int y = furniture.Y - 1; y >= 0; y--)
            {
                GUILayout.BeginHorizontal();
                location.x = -1;
                location.y = y;
                DrawLocationButton(location, "L" + (y+1));
                GUILayout.FlexibleSpace();
                location.x = furniture.X;
                location.y = y;
                DrawLocationButton(location, "R" + (y + 1));
                GUILayout.EndHorizontal();
            }

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            for (int x = 0; x < furniture.X; x++)
            {
                location.x = x;
                location.y = -1;
                DrawLocationButton(location, "B" + (x + 1));
            }
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            DrawDefaultInspector();
        }

        private void DeselectAll()
        {
            var location = new Vector2(0, 0);
            for (int x = 0; x < furniture.X; x++)
            {
                location.x = x;
                location.y = furniture.Y;
                furniture.RemoveLocation(location);

                location.x = x;
                location.y = -1;
                furniture.RemoveLocation(location);
            }

            for (int y = furniture.Y - 1; y >= 0; y--)
            {
                location.x = -1;
                location.y = y;
                furniture.RemoveLocation(location);

                location.x = furniture.X;
                location.y = y;
                furniture.RemoveLocation(location);
            }

            EditorUtility.SetDirty(furniture);
        }

        private void SelectAll()
        {
            var location = new Vector2(0, 0);
            for (int x = 0; x < furniture.X; x++)
            {
                location.x = x;
                location.y = furniture.Y;
                furniture.AddLocation(location);

                location.x = x;
                location.y = -1;
                furniture.AddLocation(location);
            }

            for (int y = furniture.Y - 1; y >= 0; y--)
            {
                location.x = -1;
                location.y = y;
                furniture.AddLocation(location);

                location.x = furniture.X;
                location.y = y;
                furniture.AddLocation(location);
            }

            EditorUtility.SetDirty(furniture);
        }
    }
}
